package com.ygqh.common.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/3/21 15:31
 * @since 1.0.0
 */
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
        super.addFormatters(registry);
    }

    /**
     * 1.  引入fastJson依赖
     * 2. 自定义WebMvcConfigurer并继承WebMvcConfigurerAdapter
     * 3. 重写configureMessageConverters方法
     * 4. 自定义配置FastJsonConfig
     * 5. 将FastJsonHttpMessageConverter添加到HttpMessageConverter
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new LocalFastJsonHttpMessageConverter();
        converters.add(converter);
    }
}
